// pages/addAdress/addAdress.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recipient:'',
    tel:'',
    addresDetail:'',
    provinces: [],//省份数据
    province:'',
    provinceId:'1',
    citys:[],//城市数据
    city:'',
    cityId:'1',
    countys:[],//区县数据
    county:'',
    countyId:'1',
    tag:false,//城市三级联动是否显示
    guid:'',//判断是新添加地址还是编辑地址
    isDefault:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("地址栏信息",options)
    const _this = this
    if (options.guid){
      //编辑地址
      wx.request({
        url: app.globalData.url +'/area/address',
        method:'post',
        header:{
          'content-type': 'application/x-www-form-urlencoded',
          "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
          "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
        },
        success(res){
          res.data.addressList.forEach(function(value,index) {
            if (value.guid == options.guid){
              _this.setData({
                recipient: value.userName,
                tel: value.mobile,
                addresDetail: value.address,
                province: value.provinceName,
                city: value.cityName,
                county: value.countyName,
                provinceId: value.provinceId,
                cityId: value.cityId,
                countyId: value.countyId
              })
            }
          })
        }
      })
    }else{
      _this.getCity(1)
      _this.getCounty(1)
    } 
    wx.request({
      url: app.globalData.url + '/area/list/province',
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
        "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
      },
      success(res) {
        console.log('省份信息', res)
        _this.setData({
          provinces: res.data.provinces
        })
      }
    })   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log('页面隐藏')
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // console.log('添加地址页面卸载')
    // wx.navigateTo({
    //   url: '/pages/addAdress/addAdress',
    // })
    wx.showModal({
      title: '',
      content: '是否保存本次编辑结果',
      confirmText:'删除',
      cancelColor:'#DC2D2E',
      confirmColor:'#454545',
      success(res){
        if(res.confirm){
          wx.redirectTo({
            url: '/pages/address/address',
          })
        }else if(res.cancel) {
          wx.redirectTo({
            url: '/pages/addAdress/addAdress',
          })
        }
        
      }
    })
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  recipientChange(res) {
    this.setData({
      recipient:res.detail.value
    })
  },
  telChange(res) {
    this.setData({
      tel:res.detail.value
    }) 
  },
  addresDetailChange(res) {
    this.setData({
      addresDetail:res.detail.value
    })
  },
  addressChange(res){
    this.setData({
      tag:!this.data.tag
    })
  },
  //获取城市
  getCity(id){
    let _this = this
    wx.request({
      url: app.globalData.url + '/area/list/city',
      method:'post',
      header:{
        'content-type': 'application/x-www-form-urlencoded',
        "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
        "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
      },
      data:{
        provinceId:id
      },
      success(res){
        console.log("城市信息",res)
        _this.setData({
          citys:res.data.citys,
          city: res.data.citys[0].cityName,
          cityId: res.data.citys[0].cityId
        })
        _this.getCounty(_this.data.cityId)
      }
    })
  },
  //获取区县
  getCounty(id){
    const _this = this
    wx.request({
      url: app.globalData.url +'/area/list/county',
      method:'post',
      header:{
        'content-type': 'application/x-www-form-urlencoded',
        "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
        "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
      },
      data:{
        cityId:id
      },
      success(res){
        console.log("区县信息",res)
        _this.setData({
          countys: res.data.countys,
          county: res.data.countys[0].countyName,
          countyId: res.data.countys[0].countyId
        })
      }
    })
  },
  //省份变化时方法
  provinceChange(res){  
    this.setData({
      provinceId: this.data.provinces[res.detail.value].provinceId,
      province: this.data.provinces[res.detail.value].provinceName
    })
    this.getCity(this.data.provinceId)
  },
  //城市变化时
  cityChange(res){
    this.setData({
      city: this.data.citys[res.detail.value].cityName,
      cityId: this.data.citys[res.detail.value].cityId
    })
    this.getCounty(this.data.cityId)
  },
  //区县变化时
  countyChange(res) {
    this.setData({
      county: this.data.countys[res.detail.value].countyName,
      countyId: this.data.countys[res.detail.value].countyId
    })
    console.log(this.data.provinceId)
    console.log(this.data.cityId)
    console.log(this.data.countyId)
  },
  // 保存或更新地址
  saveOrUpdate() {

    wx.request({
      url: app.globalData.url+'/area/saveUserAddress',
      method:'post',
      header:{
        'content-type': 'application/x-www-form-urlencoded',
        "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
        "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
      },
      data:{
        guid: this.data.guid,
        userName: this.data.recipient,
        userPhone: this.data.tel,
        provinceId: this.data.provinceId,
        cityId: this.data.cityId,
        countyId:this.data.countyId,
        address: this.data.addresDetail,
        isDefault: this.data.isDefault
      },
      success(res){
        console.log('保存地址成功',res)
        wx.navigateTo({
          url: '/pages/address/address'
        })
      }
    })
  },
  //保存新地址
  saveAddress(res){
    this.setData({
      guid:''
    })
    this.saveOrUpdate()
  }
})